home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
3_16.lha
/
3_16
/
3_16.in
< prev
next >
Wrap
Text File
|
1993-08-08
|
2KB
|
112 lines
include <stream.h>
xtern void error(char *);
include "3_16b.c" /* dolinecomment */
include "3_16c.c" /* doblockcomment */
include "3_16d.c" /* do_string_or_char */
include "3_16a.c" /* main */
/ Strip all C++ comments
/ from a source file read
/ on the standard input.
include <stream.h>
nt main(int, char**)
char ch;
while (cin.get(ch))
{
// a / may be followed by another / or a *
if (ch == '/')
{
cin.get(ch);
if (!cin)
break;
if (ch == '/') dolinecomment();
else if (ch == '*') doblockcomment();
// if not one of the above, then it's
// not a comment and the 2nd character
// must be rescanned
else
{
cout.put('/');
cin.putback(ch);
}
}
else // a printable character
{
cout.put(ch);
if (ch == '"' || ch == '\'')
do_string_or_char(ch);
}
}
return 0;
* handle a line comment */
oid dolinecomment()
char ch;
// loop until end of line
while (cin.get(ch))
if (ch == '\n')
{
cout.put(ch);
return;
}
error("EOF found within line comment");
* handle a block comment */
oid doblockcomment()
cout.put(' ');
char ch;
// loop until matching */
while (cin.get(ch))
if (ch == '*')
{
cin.get(ch);
if (ch == '/')
return;
cin.putback(ch);
}
else if (ch == '\n')
cout.put(ch);
error("EOF found within full comment");
* Copy a string or character constant. */
* Startquote is the character, either */
* ' or ", which started this constant. */
oid do_string_or_char(char startquote)
char ch;
while (cin.get(ch))
{
// copy chars within string
cout.put(ch);
// found 2nd quote?
if (ch == startqΓnt"
return;
// found \x, skip past 1 char
if (ch == '\\')
{
cin.get(ch);
if (!cin)
break;
cout.put(ch);
}
}
error("EOF found within string or char constant");